
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Diagnostics;

namespace Swaf.Container
{
	/// <summary>
	/// Summary description for BTreeDictionary.
	/// </summary>
	[Serializable]
	public class BTreeDictionary : IDictionary
	{
		private BTree m_tree;

		public BTreeDictionary()
		{
			m_tree = new BTree(false);
		}
		#region IDictionary Members

		public bool IsReadOnly
		{
			get
			{
				return false;
			}
		}

		public IDictionaryEnumerator GetEnumerator()
		{
			return new BTreeDictionaryEnumerator(m_tree);
		}

		public object this[object key]
		{
			get
			{
				return m_tree.FindObject((IComparable)key);
			}
			set
			{
				m_tree.Add((IComparable)key, value);
			}
		}

		public void Remove(object key)
		{
			m_tree.Remove((IComparable)key);
		}

		public bool Contains(object key)
		{
			return m_tree.Contains((IComparable)key);
		}

		public void Clear()
		{
			m_tree = new BTree(false);
		}

		public ICollection Values
		{
			get
			{
				// TODO:  Add BTreeDictionary.Values getter implementation
				return null;
			}
		}

		public void Add(object key, object value)
		{
			m_tree.Add((IComparable)key, value);
		}

		public ICollection Keys
		{
			get
			{
				// TODO:  Add BTreeDictionary.Keys getter implementation
				return null;
			}
		}

		public bool IsFixedSize
		{
			get
			{
				return false;
			}
		}

		#endregion

		#region ICollection Members

		public bool IsSynchronized
		{
			get
			{
				return true;
			}
		}

		public int Count
		{
			get
			{
				return m_tree.Count;
			}
		}

		public void CopyTo(Array array, int index)
		{
			// TODO:  Add BTreeDictionary.CopyTo implementation
		}

		public object SyncRoot
		{
			get
			{
				return this;
			}
		}

		#endregion

		#region IEnumerable Members

		IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return new BTreeDictionaryEnumerator(m_tree);
		}

		#endregion
	}

	[Serializable]
	public class BTreeDictionaryEnumerator : IDictionaryEnumerator
	{
		private BTreeIterator m_iterator;

		public BTreeDictionaryEnumerator(BTree tree)
		{
			m_iterator = new BTreeIterator(tree,true);
		}

		#region IDictionaryEnumerator Members

		public object Key
		{
			get
			{
				Debug.Assert(m_iterator != null);
				return m_iterator.CurrentElement.Key;
			}
		}

		public object Value
		{
			get
			{
				Debug.Assert(m_iterator != null);
				return m_iterator.CurrentElement.Value;
			}
		}

		/// <summary>
		/// Creates new DictionaryEntry object each time this is called.  Use
		/// Key and Value properties instead of this.
		/// </summary>
		public DictionaryEntry Entry
		{
			get
			{
				//TODO: Should this throw an exception or take the optimization hit?
				//For now, take the hit for compatability.
				//throw new Exception("Dont use this method");
				return new DictionaryEntry(Key,Value);
			}
		}

		#endregion

		#region IEnumerator Members

		public void Reset()
		{
			m_iterator.Reset();
		}

		public object Current
		{
			get
			{
				BTreeEntry ent = m_iterator.CurrentElement;
				Debug.Assert(ent != null);
				return new DictionaryEntry(ent.Key, ent.Value);
			}
		}

		public bool MoveNext()
		{
			return m_iterator.Next();
		}

		#endregion

	}
}
